
//
//  DiscoverMCircleDetailViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/14.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//
#import "HouseholdDetailViewController.h"
#import "RDVTabBarController.h"
#import "UIImageView+WebCache.h"
#import "HttpRequest.h"
#import "LoginViewController.h"
#import "UIButton+WebCache.h"
#import "KeyboardModel.h"
#import "ShareView.h"
#import "DiscoverHouseholdCommentsViewController.h"
//#import "ToBindCompanyViewController.h"
//#import "NewMediaPromotionViewController.h"
#import "ExhibitionObject.h"
@interface HouseholdDetailViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextViewDelegate, UIWebViewDelegate,KeyboardModelDelegate,ShareViewDelegate,TestDelegate, UITextFieldDelegate>

@end

@implementation HouseholdDetailViewController
{
    ShareView*SV;
    UIView*shareblackview;
    UIScrollView*MYmainScroll;
    UIWebView*webview;
    NSString*isCollect;
    NSDictionary*dic;
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self closeKeyBoad];
    [[self rdv_tabBarController] setTabBarHidden:YES];
    [self.navigationController setNavigationBarHidden:NO];
    

    
    
//    self.navigationController.navigationBarHidden=NO;
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
//    self.navigationController.delegate = self;
//    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
//    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
//    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
//    [self.navigationController.navigationBar addSubview:lineLabel];
//    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
//    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
//    CGRect backframe = CGRectMake(0,0,30,30);
//    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
//    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
//    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
//    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
//    UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0,100, 21)];
//    titleLabel.textAlignment=NSTextAlignmentCenter;
//    titleLabel.text=self.titleStr;
//    self.navigationItem.titleView = titleLabel;
    
    
//    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
//    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
    UIImage *img=[UIImage imageNamed:@"fenxiang"];
    UIImage *tinted = [img rt_tintedImageWithColor:[UIColor whiteColor]];
    
    UIButton* sxBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
    [sxBtn setImage:tinted forState:UIControlStateNormal];
    [sxBtn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:sxBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    self.view.backgroundColor=[UIColor whiteColor];
    
    NSLog(@"typeCode == %@", self.typeCode);
    
//    if ([self.typeCode isEqualToString:@"household"]) {  // 新媒体资讯
////        NSString *pageURL = self.pageURL;
////        [webview loadRequest:[NSURLRequest requestWithURL:[[NSURL alloc] initWithString:pageURL]]];
//        [self setup];
//    } else if ([self.typeCode isEqualToString:@"scene"]){ // 新媒体橱窗
//        NSString *pageURL = self.pageURL;
//        [webview loadRequest:[NSURLRequest requestWithURL:[[NSURL alloc] initWithString:pageURL]]];
//    } else if ([self.typeCode isEqualToString:@"custom"]){ // 新媒体自定义
//        NSString *pageURL = self.pageURL;
//        [webview loadRequest:[NSURLRequest requestWithURL:[[NSURL alloc] initWithString:pageURL]]];
//    } else { //其他
//        [self setup];
//    }
    
    
    
    
}

-(void)closeKeyBoad {
    
    [self.window makeKeyWindow];
    self.window.hidden=YES;
    [SV removeFromSuperview];
   
}
-(void)cancel:(NSString *)cancel{
    [self closeKeyBoad];
}

-(void)share:(UIButton*)sender{
    //Household资讯 scene橱窗 自定义custom
//    NSString *contentType = nil;
//    if ([self.typeCode isEqualToString:@"household"]) {  // 资讯
//        contentType = @"Household";
//    } else if ([self.typeCode isEqualToString:@"scene"]){ // 橱窗
//        contentType = @"scene";
//    } else if ([self.typeCode isEqualToString:@"custom"]){ // 自定义
//        contentType = @"custom";
//    } else { //其他
//        contentType = @"Household";
//    }
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSString* companyId = [user objectForKey:@"companyId"];
    NSString* fid = [user objectForKey:@"fid"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"Household" forKey:@"contentType"];
    [param setValue:self.fid forKey:@"contentId"];
    [param setValue:@"wx_single" forKey:@"shareTarget"];
    [param setValue:fid forKey:@"staffId"];
    [param setValue:companyId forKey:@"companyId"];
    [param setValue:self.srId forKey:@"srId"];
    NSLog(@"param ==== %@",param);
    [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self.view];
    [HttpRequest requestData:@"media/getShareUrl" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        NSLog(@"====%@",severDataDict.dataDict);
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
                [self.window makeKeyAndVisible];
                
                shareblackview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]; 
                shareblackview.backgroundColor=[UIColor blackColor];
                shareblackview.alpha=0.65;
                [self.window addSubview:shareblackview];

//                SV = [[ShareView alloc]initWithFrame:CGRectMake(0, ScreenHeight-245, ScreenWidth,245)];
                
                SV = [[ShareView alloc] init];
                if (is_iPhoneX) {
                    SV.frame = CGRectMake(0, ScreenHeight-245-34, ScreenWidth,245+34);
                } else{
                    SV.frame = CGRectMake(0, ScreenHeight-245, ScreenWidth,245);
                }
                SV.dataDict=severDataDict.dataDict;
                
                
//                // 新媒体推广的时候需要分享后调用分享成功方法
//                if ([self.typeCode isEqualToString:@"household"] ||
//                    [self.typeCode isEqualToString:@"scene"] ||
//                    [self.typeCode isEqualToString:@"custom"]) {  // 资讯
//                    SV.type = @"NewMedia";
//                } else{
//                    SV.type = @"";
//                }
                
//                SV.shareUrl=[NSString stringWithFormat:@"%@&companyId=%@&staffId=%@",[severDataDict.dataDict objectForKey:@"shareUrl"],companyId,fid];
                
                SV.shareUrl= [NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"shareUrl"]];
                SV.mDelegate=self;
                [self.window addSubview:SV];
                
                UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
                singleFingerOne.numberOfTouchesRequired = 1; //手指数
                singleFingerOne.numberOfTapsRequired = 1; //tap次数
                singleFingerOne.delegate = self;
                [shareblackview addGestureRecognizer:singleFingerOne];

                
            }
            
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}

//分享成功后回调登记接口
-(void)successfullyReturns:(NSString *)successfullyReturns{

    // 新媒体推广的时候需要分享后调用分享成功方法
    if ([self.typeCode isEqualToString:@"household"] ||
        [self.typeCode isEqualToString:@"scene"] ||
        [self.typeCode isEqualToString:@"custom"]) {  // 资讯
        [self closeKeyBoad];
        [MBProgressHUD showTextHudSub:@"分享成功！" view:self.view];
        [self popularizeAdd];
    } else{
        [self closeKeyBoad];
        [MBProgressHUD showTextHudSub:@"分享成功！" view:self.view];
    }
    
}

- (void)popularizeAdd {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.srId forKey:@"srId"];
    [param setValue:self.fid forKey:@"contextId"];
    [param setValue:self.userId forKey:@"userId"];
//    [param setValue:companyId forKey:@"userIp"];
    [param setValue:@"forwarding" forKey:@"typeCode"];
    
    [MBProgressHUD showIndeterminateHudSub:@"" delay:30 view:self.view];
    NSLog(@"param == %@", param);
    [HttpRequest requestData:@"MediaPromotion/popularize/add" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        NSLog(@"====%@",severDataDict.dataDict);
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                
//                [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
//                [Global showTextHudSub:@"分享成功！" view:self.view];
//                [self closeKeyBoad];
//                NewMediaPromotionViewController *loginVC=[[NewMediaPromotionViewController alloc] init];
//                [self.navigationController pushViewController:loginVC animated:YES];
                
            }else{
                [MBProgressHUD hideProgressHUDInternal:self.view];
//                [self closeKeyBoad];
//                [Global showTextHudSub:@"分享失败！" view:self.view];
//                [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
            }
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
//            [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
//            [self closeKeyBoad];
//            [Global showTextHudSub:@"分享失败！" view:self.view];
        }
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";

    [self setup];
    
    [webview removeFromSuperview];
    webview = [[UIWebView  alloc]  initWithFrame:CGRectMake( 0, 0,  ScreenWidth ,  ScreenHeight-64)];
    webview.scalesPageToFit = TRUE;
    [webview setUserInteractionEnabled:YES];     //是否支持交互
    [webview setDelegate:self];                 //委托
    [webview setOpaque:YES];
    webview.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:webview];


}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    JSContext *context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
//    //js调用iOS
//    //第一种情况
//    //其中test1就是js的方法名称，赋给是一个block 里面是iOS代码
//    ExhibitionObject *exhiObject=[ExhibitionObject new];
//    exhiObject.delegate=self;
//    context[@"appJs"]=exhiObject;

}

- (void)setup{
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    [params setValue:self.fid forKey:@"infoId"];
    NSLog(@"-----%@",params);
    [MBProgressHUD showIndeterminateHudSub:@"" delay:20 view:self.view];
    [HttpRequest requestData:@"media/Household/Detail" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
        
                NSLog(@"-----%@",severDataDict.dataDict);
                NSString * path =[NSString stringWithFormat:@"%@",[severDataDict.dataDict objectForKey:@"infoPage"]];
                NSURL *url = [NSURL URLWithString:path];
                [webview loadRequest:[NSURLRequest requestWithURL:url]]; 
                isCollect=[severDataDict.dataDict objectForKey:@"isCollect"] ;
                dic=severDataDict.dataDict;
                [[KeyboardModelboard customKeyboard] textViewShowView:self String1:[severDataDict.dataDict objectForKey:@"isCollect"] pinglunshuString1 :[[severDataDict.dataDict objectForKey:@"householdInfo"]objectForKey:@"commentCount"]  customKeyboardDelegate:self];
                
            }else{
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    LoginViewController*detailViewController = [[LoginViewController alloc] init];
                    [self.navigationController pushViewController:detailViewController animated:YES];
                }
            }
            
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
        }
    }];
}



- (void)textFieldDidEndEditing:(UITextField *)textField{
    
}
// 评论输入完成
-(void)pinglunBtn:(UITextField *)textViewGet {
    NSLog(@"%@",textViewGet.text);
    if (textViewGet.text.length==0) {
        [TSAlertTool showAlertMessage:@"内容为空"];
    }else{
        if ([textViewGet.text isEqualToString:@"我来说两句..."]) {
            [TSAlertTool showAlertMessage:@"内容为空"];
        }else{
            NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
            NSString* tokenId = [user1 objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:tokenId forKey:@"tokenId"];
            [params setValue:self.fid forKey:@"infoId"];
            [params setValue:textViewGet.text forKey:@"content"];
            NSLog(@"-----%@",params);
            [MBProgressHUD showIndeterminateHudSub:@"" delay:10 view:self.view];
            [HttpRequest requestData:@"media/Household/AddComment" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    [MBProgressHUD hideProgressHUDInternal:self.view];
                    NSLog(@"-----%@",severDataDict.dataDict);
                    textViewGet.text=@"";
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        DiscoverHouseholdCommentsViewController*detailViewController = [[DiscoverHouseholdCommentsViewController alloc] init];
                        detailViewController.fid=self.fid;
                        detailViewController.isCollect=isCollect;
                        [self.navigationController pushViewController:detailViewController animated:YES];
                    }
                } else {
                    [MBProgressHUD hideProgressHUDInternal:self.view];
                    [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
                }
            }];
        }
    }
}


// 点击评论按钮
-(void)pinglunBtnjiemian:(NSString *)pinglunjiemian{
   
    DiscoverHouseholdCommentsViewController*detailViewController = [[DiscoverHouseholdCommentsViewController alloc] init];
    detailViewController.fid=self.fid;
      detailViewController.isCollect=isCollect;
    [self.navigationController pushViewController:detailViewController animated:YES];
}

//点击收藏
-(void)guangzhuBtn:(UIButton *)guanzhu {
    
    NSLog(@"%@",guanzhu.titleLabel.text);
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    [params setValue:self.fid forKey:@"infoId"];
    
    NSLog(@"-----%@",params);
    [MBProgressHUD showIndeterminateHudSub:@"" delay:10 view:self.view];
    [HttpRequest requestData:@"media/Household/Collect" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            NSLog(@"-----%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
            
                [[KeyboardModelboard customKeyboard]textViewShowView:self String1:[severDataDict.dataDict objectForKey:@"isCollect"] pinglunshuString1 :[[dic objectForKey:@"householdInfo"]objectForKey:@"commentCount"]    customKeyboardDelegate:self];
                    isCollect=[severDataDict.dataDict objectForKey:@"isCollect"] ;
            }else{
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                
            }
            
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}



@end
